#ifndef TRAFFIC_JAM_QUEUE_HPP
#define TRAFFIC_JAM_QUEUE_HPP

#include <deque>
#include "direction.hpp"
#include "cars.hpp"
#include "pthreadwrapper.hpp"

namespace jam {

template<direction_t dir, car_logic_t logic>
class Queue: public std::deque<Car<dir, logic>> {
 public:
  typedef Car<dir, logic> car_t;
  const static direction_t direction = dir;
  void inline wait4_car(car_t &car) {
    thread::join(car.thread);
  }
  void add_car(car_id_t id) {
    this->push_back(car_t(id));
  }
  void emit() {
    if (this->empty()) {
      return;
    }
    car_t &first_car = this->front();
    first_car.emit();
    wait4_car(first_car);
    this->pop_front();
  }
  inline bool has_car() const {
    return !this->empty();
  }
};

}

#endif //TRAFFIC_JAM_QUEUE_HPP
